//$Id$

package builders;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Automated Test Driver for testing Logic and its interactions with the other classes
 * 
 * @author Yuling
 */
public class ATD {
	Logic logic;
	String inputFile;
	String expectedOutputFile;
	String resultsFile;
	String tileManager;
	ArrayList<TestCase> testCases = new ArrayList<TestCase>();
	String PARTITION_STRING = "*****"; // PARTITION_STRING between expectedOutput for better viewing
	
	/**
	 * Constructor for ATD
	 * 
	 * @param inputFile     		 name of the input file
	 * @param expectedOutputFile 	name of the expectedOutput file
	 * @param resultsFile 			name of the results file that will be overwritten/written to
	 */
	public ATD (String inputFile, String expectedOutputFile, String resultsFile) {
		logic = new Logic();
		this.inputFile = inputFile;
		this.expectedOutputFile = expectedOutputFile;
		this.resultsFile = resultsFile;
	}
	
	// the only thing you can test is Logic's execute command
	/**
	 * Main method - execute it to test your testCases as specified in your various files here
	 */
	public static void main(String[] args) throws Exception {
		String workingDir = System.getProperty("user.dir");
		String inputFile = workingDir + "\\src\\builders\\testcases.txt";
		String expectedOutputFile = workingDir + "\\src\\builders\\expectedOutput.txt";
		String resultsFile = workingDir +"\\src\\builders\\testresults.txt";
		ATD atd = new ATD(inputFile, expectedOutputFile, resultsFile);
		atd.tileManager = "defaultTile.txt";
		atd.readTestCases();
		atd.executeTests();
		atd.writeResults();
		System.out.println("'done");
	}

	/**
	 * Reads the testCases + their expected Outputs and creates a testCase from there
	 * testCase is added on to the list of testCases in ATD
	 */
	private void readTestCases() {
		File input = new File(inputFile);
		File expectedOutput = new File(expectedOutputFile);
		
		try {
			FileReader fileInputReader = new FileReader(input);
	        BufferedReader inputBR = new BufferedReader (fileInputReader);
	        
	        FileReader fileOutputReader = new FileReader(expectedOutput);
	        BufferedReader outputBR = new BufferedReader(fileOutputReader);
	        
	        String lineFromInput = null;
	        String lineFromOutput = null;
	        String nextLine = null;
	        
	        try {
	        	// reads two lines at once for input files, and a chunk of information for expectedOutputFile
				while ((lineFromInput = inputBR.readLine()) != null){
					lineFromOutput = outputBR.readLine(); 
					nextLine = outputBR.readLine();
					while (! nextLine.equals(PARTITION_STRING)){
						lineFromOutput += "\n" + nextLine;
						nextLine = outputBR.readLine();
					}
					TestCase tc = new TestCase(lineFromInput, inputBR.readLine(), lineFromOutput);
					testCases.add(tc);
				}
				inputBR.close();
				outputBR.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * To iterate through the testCases and execute them one by one
	 */
	private void executeTests() {
		logic.setTileManager(tileManager);
		// int i = 1;
		for (TestCase tc : testCases){
			// System.out.println("Test case "+i+" is executing.");
			tc.verifyResult(logic.execute(tc.getMessageObject()));
			// i++;
		}
	}

	/**
	 * Writing of results into results file
	 */
	private void writeResults() {
		try {
		    BufferedWriter out = new BufferedWriter(new FileWriter(resultsFile));
		    int testCase = 1;
		    int passed = 0;
		    for (TestCase tc : testCases){
		    	Boolean result = tc.result();
		    	if (result){
		    		out.write("Test Case " + testCase + ": True\n");
		    		passed++;
		    	} else {
		    		out.write("Test Case " + testCase + ": False\n");
		    		out.write(tc.getExpectedOutput() + "\n");
		    		out.write(tc.getGameStateStatus() + "\n");
		    		/*String[] expectedOutput = tc.getExpectedOutput().split("\n");
		    		String[] realOutput = tc.getGameStateStatus().split("\n");
		    		
		    		System.out.println(expectedOutput.length +"" + realOutput.length);
		    		for (int i = 0; i < expectedOutput.length; i++){
		    			if (! (expectedOutput[i].equals(realOutput[i]))){
		    				out.write(expectedOutput[i] + "\n" + realOutput[i] + "\n");
		    			}
		    		}*/
		    		//TODO: Better implementation so that people can see the files better
		    		/*
		    		String[] expectedOutput = tc.getExpectedOutput().split("\n");
		    		String[] realOutput = tc.getGameStateStatus().split("\n");
		    		int currentLine = 1;
		    		 
		    		 for (String expected : expectedOutput){
		    			for (int lineToIterate = currentLine; lineToIterate < realOutput.length; lineToIterate++){
		    				if (expected.equals(realOutput[lineToIterate])){
		    					
		    				}
		    			}
		    		}
		    		*/
		    	}
			    testCase++;
		    }
		    out.write("TestCase Statistics: " + passed + "/" + (testCase - 1) + " passed.");
		    out.close();
		} catch (IOException e) {
		}
	}
	
}
